<!-- 个人中心 -->
<template>
  <div class="app-container">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <span>个人信息</span>
          <el-button type="primary" @click="handleEdit">修改信息</el-button>
        </div>
      </template>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="用户名">
          {{ userInfo.username }}
        </el-descriptions-item>
        <el-descriptions-item label="真实姓名">
          {{ userInfo.realName }}
        </el-descriptions-item>
        <el-descriptions-item label="角色">
          {{ userInfo.role === "teacher" ? "教师" : "学生" }}
        </el-descriptions-item>
        <el-descriptions-item label="班级">
          {{ userInfo.className || "-" }}
        </el-descriptions-item>
        <el-descriptions-item label="邮箱">
          {{ userInfo.email || "-" }}
        </el-descriptions-item>
        <el-descriptions-item label="手机号">
          {{ userInfo.phone || "-" }}
        </el-descriptions-item>
      </el-descriptions>
    </el-card>

    <!-- 修改信息对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="修改个人信息"
      width="500px"
      @close="resetForm"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="profile-form"
      >
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input
            v-model="form.oldPassword"
            type="password"
            placeholder="请输入旧密码"
            show-password
          />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="form.newPassword"
            type="password"
            placeholder="请输入新密码"
            show-password
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            type="password"
            placeholder="请确认新密码"
            show-password
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { getUserInfo, updateProfile } from "@/api/user";

const userInfo = ref({});
const dialogVisible = ref(false);
const formRef = ref();

// 表单数据
const form = reactive({
  realName: "",
  email: "",
  phone: "",
  oldPassword: "",
  newPassword: "",
  confirmPassword: "",
});

// 表单验证规则
const rules = {
  realName: [{ required: true, message: "请输入真实姓名", trigger: "blur" }],
  email: [
    { required: false, message: "请输入邮箱", trigger: "blur" },
    { type: "email", message: "请输入正确的邮箱格式", trigger: "blur" },
  ],
  phone: [
    { required: false, message: "请输入手机号", trigger: "blur" },
    {
      pattern: /^1[3-9]\d{9}$/,
      message: "请输入正确的手机号格式",
      trigger: "blur",
    },
  ],
  oldPassword: [{ required: true, message: "请输入旧密码", trigger: "blur" }],
  newPassword: [
    { required: true, message: "请输入新密码", trigger: "blur" },
    { min: 6, message: "密码长度不能小于6位", trigger: "blur" },
  ],
  confirmPassword: [
    { required: true, message: "请确认新密码", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value !== form.newPassword) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
};

// 获取用户信息
const fetchUserInfo = async () => {
  try {
    const  data  = await getUserInfo();
    userInfo.value = data;
  } catch (error) {
    console.error("获取用户信息失败：", error);
  }
};

// 打开编辑对话框
const handleEdit = () => {
  form.realName = userInfo.value.realName;
  form.email = userInfo.value.email;
  form.phone = userInfo.value.phone;
  dialogVisible.value = true;
};

// 重置表单
const resetForm = () => {
  formRef.value?.resetFields();
  form.oldPassword = "";
  form.newPassword = "";
  form.confirmPassword = "";
};

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return;

  await formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        await updateProfile({
          realName: form.realName,
          email: form.email,
          phone: form.phone,
          oldPassword: form.oldPassword,
          newPassword: form.newPassword,
        });

        ElMessage.success("修改成功");
        dialogVisible.value = false;
        fetchUserInfo();
      } catch (error) {
        console.error("修改失败：", error);
      }
    }
  });
};

onMounted(() => {
  fetchUserInfo();
});
</script>

<style scoped>
.profile-card {
  max-width: 800px;
  margin: 0 auto;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.profile-form {
  padding: 20px;
}

:deep(.el-descriptions__label) {
  width: 100px;
  justify-content: flex-end;
}
</style>
